package com.onepower.admin.controller;

import com.onepower.admin.config.RedisConfig;
import com.onepower.core.domain.AuthUser;
import com.onepower.core.modules.service.ISysUserService;
import com.onepower.core.result.BaseResultCode;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.JsonUtil;
import com.onepower.core.utils.ResultUtils;
import com.onepower.core.utils.SecurityUtil;
import com.onepower.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@RequestMapping("/api/common/")
public class LoginController {

    @Autowired
    private RedisTemplate<Serializable, Object> redisTemplate;

    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public CommonResult login(@RequestParam(value = "account", required = true) String account,
                              @RequestParam(value = "password", required = true) String password
                              ){
        AuthUser authUser = sysUserService.login(account,password);
        if (authUser != null){
            String tonkenStr = JsonUtil.getInstance().object2JSON(authUser);
            String tonken = SecurityUtil.getEncryptString(tonkenStr,"tkdMatch");
        //    redisTemplate.opsForValue().set();
            return ResultUtils.ok(tonken);
        }
        return  ResultUtils.error(BaseResultCode.ACCOUNT_PASSWORD_ERROR);

    }

}
